package com.sacc.service.Impl;

import com.power.common.model.CommonResult;
import com.sacc.mapper.ProductInfoAllMapper;
import com.sacc.pojo.ProductInfoAll;
import com.sacc.service.ProductInfoAllService;
import com.sacc.service.ProductTypeService;
import com.sacc.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author 滚~韬
 * @date 2022/4/9 16:19
 */
@Service
public class ProductInfoAllServiceImpl implements ProductInfoAllService {
//    @Autowired
//    private FileUtil fileUtil;
    @Autowired
    private ProductInfoAllMapper productInfoAllMapper;
    @Autowired
    private ProductTypeService productTypeService;
    private final int RUNSIZE =8;

    /**
     * 获取单一产品详细信息
     * @param productId
     * @return
     */
    @Override
    public ProductInfoAll GetOneInfo(Integer productId) {
        return  productInfoAllMapper.selectById(productId);
    }
    /**
     * 获取单一产品列表信息
     * @param productId
     * @return
     */
    @Override
    public ProductInfoAll GetOneList(Integer productId) {
        ProductInfoAll productInfoAll = productInfoAllMapper.selectById(productId);
        productInfoAll.setProductTypeName(productTypeService.GetName(productId));
        return productInfoAll;
    }
    /**
     * 返回产品列表
     * @return
     */
    @Override
    public List<ProductInfoAll> GetAll() {
        ExecutorService executor = Executors.newFixedThreadPool(RUNSIZE);
        Integer number =  productInfoAllMapper.selectCount(null);
        int yu = number % RUNSIZE;
        number+=RUNSIZE-yu;
        int count = number / RUNSIZE;
        List<ProductInfoAll> productInfoAllList = new ArrayList<>();
        for (int i = 0; i < RUNSIZE; i++) {
            int index = i*count;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try{

                        for(int l = index;l<index+count;l++){
                            ProductInfoAll productInfoAll = productInfoAllMapper.selectById(l + 1);
                            if(productInfoAll!=null)
                                productInfoAllList.add(GetOneList(productInfoAll.getProductId()));
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });

        }
        executor.shutdown();
        //等待线程完毕
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
            return productInfoAllList;
        } catch (InterruptedException e) {
            executor.shutdownNow();
        }
        return productInfoAllList;
    }

    /**
     * 添加产品单一信息
     * @param productInfoAll,file
     * @return
     */
    @Override
    public CommonResult AddOne(ProductInfoAll productInfoAll, MultipartFile[] files){
        try{
//            String image = fileUtil.upload("/product/",file,"image");
            CommonResult commonResult = OssUtil.Uploads(files);
            productInfoAll.setCreateTime(LocalDateTime.now());
            productInfoAll.setImageUrl( commonResult.getData().toString().replaceFirst(",",""));
            productInfoAllMapper.insert(productInfoAll);
            return  commonResult;
        }catch (Exception e){
            return  new CommonResult("400","添加产品错误"+e.toString());
        }
    }

    @Override
    public CommonResult UpdateOne(Integer productId, MultipartFile[] files) {
        try{
//           String image = fileUtil.upload("/product/",file,"image");
            ProductInfoAll productInfoAll = productInfoAllMapper.selectById(productId);
            String[] split = productInfoAll.getImageUrl().split(",");
            for(String s : split){
                OssUtil.Delete(s);
            }
            CommonResult commonResult = OssUtil.Uploads(files);
            productInfoAll.setCreateTime(LocalDateTime.now());
            productInfoAll.setImageUrl( commonResult.getData().toString().replaceFirst(",",""));
            productInfoAllMapper.updateById(productInfoAll);
            return  commonResult;
        }catch (Exception e){
            return  new CommonResult("400","更新产品错误"+e.toString());
        }
    }
}
